using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MCMLXVII.Tools.OrganizationChart.MembersClasses;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;

namespace MCMLXVII.Tools.OrganizationChart.SmartEditors
{
    public partial class ucSmartGridEditor : ucSmartBaseEditor 
    {
        public override event EventHandler<MemberUpdatedEventsArgs> EditorClosed;
        public override event EventHandler<MemberUpdatedEventsArgs> MemberUpdated;

        private int _MemberID = 0;
        private int _NodeID = 0;
        public override int NodeID
        {
            get
            {
                return _NodeID;
            }
        }
/*        public ucSmartGridEditor(object EditingObject):base()
        {
            InitializeComponent();
            _EditingObject = EditingObject ;
            base.CancelRequested += new EventHandler(ucSmartEditor_CancelRequested);
            base.SaveRequested += new EventHandler(ucSmartEditor_SaveRequested);
            if (EditingObject is MemberBase)
            {
                _MemberID = ((MemberBase)EditingObject).MemberID;
            }
            else
            {
                _MemberID = ((NodeEditorHelper)EditingObject).ID;
            }
            SelectedItemProperties.SelectedObject = EditingObject;

        }
*/
        public ucSmartGridEditor(object EditingObject, int NodeID): base()
        {
            _NodeID = NodeID;
            InitializeComponent();
            base.CancelRequested += new EventHandler(ucSmartEditor_CancelRequested);
            base.SaveRequested += new EventHandler(ucSmartEditor_SaveRequested);
            if (EditingObject is MemberBase)
            {
                _MemberID = ((MemberBase)EditingObject).MemberID;
            }
            else
            {
                _MemberID = ((NodeEditorHelper)EditingObject).ID;
            }
            SelectedItemProperties.SelectedObject = EditingObject;
        }


        void ucSmartEditor_SaveRequested(object sender, EventArgs e)
        {                      
            if (MemberUpdated != null)
            {
                if (SelectedItemProperties.SelectedObject != null)
                {
                    if (SelectedItemProperties.SelectedObject.GetType() == typeof(NodeEditorHelper)) 
                    {
                        NodeEditorHelper neh = (NodeEditorHelper)SelectedItemProperties.SelectedObject;
                        MemberUpdated.Invoke(this, new MemberUpdatedEventsArgs(_NodeID, neh));
                        base.DisableUpdate();
                    }
                    else
                    { 
                        if (((iOrganizationTreeMemberClass)SelectedItemProperties.SelectedObject).HasChanges())
                        {
                            iOrganizationTreeMemberClass EditingObject = (iOrganizationTreeMemberClass)SelectedItemProperties.SelectedObject;
                            MemberUpdated.Invoke(this, new MemberUpdatedEventsArgs(EditingObject.MemberID, _NodeID, EditingObject));
                            base.DisableUpdate();                            
                        }
                    }
                }                                        
            }
        }

        void ucSmartEditor_CancelRequested(object sender, EventArgs e)
        {
            if (EditorClosed != null)
            {
                EditorClosed.Invoke(this, new MemberUpdatedEventsArgs(_MemberID));
            }
        }


        private void ucSmartNodeEditor_Load(object sender, EventArgs e)
        {
            if (SelectedItemProperties.SelectedObject is MemberBase)
            {
                base.pboxItemClass.Image = (System.Drawing.Image)((MemberBase)SelectedItemProperties.SelectedObject).GetIcon().ToBitmap();
            }
            if (this.Width > SelectedItemProperties.Left + 50)
                SelectedItemProperties.Width = this.Width - SelectedItemProperties.Left - 50;
            if (this.Height > SelectedItemProperties.Top + 50)
                SelectedItemProperties.Height = this.Height - SelectedItemProperties.Top - 50;
            base.RelocateButtons(SelectedItemProperties.Top + SelectedItemProperties.Height);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

        }

        private void SelectedItemProperties_Click(object sender, EventArgs e)
        {

        }

        private void ucSmartGridEditor_Resize(object sender, EventArgs e)
        {
            if (this.Width > SelectedItemProperties.Left + 50)
                SelectedItemProperties.Width = this.Width - SelectedItemProperties.Left - 50;
            if (this.Height > SelectedItemProperties.Top + 50)
                SelectedItemProperties.Height = this.Height - SelectedItemProperties.Top - 50;
            base.RelocateButtons(SelectedItemProperties.Top + SelectedItemProperties.Height);
        }

        private void SelectedItemProperties_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            base.EnableUpdate();
        }
    }
}
